﻿using System;   
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;


//Base Class for all kind of Sprites
//Like AnimatedSprite, PhysicsSprite, Menu etc...!
//You can think of it as a Texture with a position and rotation at its basic
namespace Imagine
{
    public enum eHotSpot { eTopLeft, eCenter, eBottomLeft, eBottomCenter };
    enum SpriteType
    {
        eSimple,
        eAnimated,
        ePhysics,
    }

    public class Sprite
    {
        //Everything here is protected because we need to give this base functionality to all other kinds of Sprites
        public Sprite(string texture) 
        {
            //Set default Parameters for Sprite
            Texture = TheAssetManager.Instance().Load<Texture2D>(texture);
            m_color = Color.White;
            m_scale = new Vector2(1.0f,1.0f);
            m_spriteEffect = SpriteEffects.None;
            m_angle = 0.0f;
            m_position = new Vector2(0,0);
            m_origin = new Vector2(0, 0);
            m_hotSpot = eHotSpot.eCenter;
        }

        public virtual bool Draw(GameTime time)    
        {
            if(m_texture != null)
                Engine.Instance().SpriteBatch.Draw(m_texture, m_position, m_sourceRectangle, m_color, m_angle, m_origin, m_scale, m_spriteEffect, 0);

            return true;
        }
        public virtual bool Update(GameTime time)   { return true;}
        public virtual bool Release()               { return true;}

        //Getters and Setters

        #region Properties
        
        public Vector2 Position
        {
            get { return m_position;         }
            set { m_position = value;        }
        }

        public float Angle
        {
            get { return m_angle;         }
            set { m_angle = value;        }
        } 
        //Reset the Source Rectangle as well
        //Setting a texture will Reset the Source Rectangle, so take care of that
        public Texture2D Texture
        {
            get { return m_texture;         }
            set { 
                    m_texture = value; 
                    FrameSize = new Vector2(m_texture.Width,m_texture.Height);       
                }
        }
        
        public Vector2 Origin
        {
            get { return m_origin;          }
            set { m_origin = value;         }
        }

        public Vector2 Scale
        {
            get { return m_scale;           }
            set { m_scale = value;          }
        }

        public Color Color
        {
            get { return m_color;           }
            set { m_color = value;          }
        }

        public SpriteEffects SpriteEffect
        {
            get { return m_spriteEffect;    }
            set { m_spriteEffect = value;   }
        }
        //Reset the origin everytime as well 
        public Vector2 FrameSize
        {
            get { return new Vector2(m_sourceRectangle.Width, m_sourceRectangle.Height); }
            set { m_sourceRectangle.Width = (int)value.X;     
                  m_sourceRectangle.Height = (int)value.Y;  
                  ChangeHotSpot();
                }
        }

        public eHotSpot HotSpot
        {
            get { return m_hotSpot;  }
            set 
            { 
                m_hotSpot = value; 
                ChangeHotSpot();
            }
        }

        #endregion
        protected void ChangeHotSpot()
        {       
            switch( m_hotSpot )
            {
                case eHotSpot.eCenter:    m_origin = new Vector2(m_texture.Width / 2, m_texture.Height / 2);  break;
                case eHotSpot.eTopLeft:   m_origin = new Vector2(0, 0);                                       break;
                case eHotSpot.eBottomLeft:m_origin = new Vector2(0, m_texture.Height);                        break;
                case eHotSpot.eBottomCenter: m_origin = new Vector2( m_texture.Width / 2, m_texture.Height ); break;
            }
        }
       
        protected eHotSpot                m_hotSpot;
        protected Texture2D               m_texture;        //Just testing, will actually be placed in Resource Manager
        protected Vector2                 m_position;
        protected Vector2                 m_origin;         //This origin is for drawing, by default its same as Box2D's
        protected Vector2                 m_scale;
        protected Rectangle               m_sourceRectangle;
        protected Color                   m_color;
        protected SpriteEffects           m_spriteEffect;
        protected float                   m_angle;
        //Will contain much more data like Sprite Effect, sourcerectangle, color, etc


    }

}
